package com.ruoyi.app.dyh.hxf.service.Impl;

import com.ruoyi.app.dyh.hxf.consts.ActiveConsts;
import com.ruoyi.app.dyh.hxf.domain.Active;
import com.ruoyi.app.dyh.hxf.mapper.ActiveMapper;
import com.ruoyi.app.dyh.hxf.service.IActiveService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActiveService implements IActiveService {

    @Autowired
    private ActiveMapper activeMapper;

    @Override
    public int deleteActive(String activeId) {
        return activeMapper.deleteActive(activeId);
    }

    @Override
    public int updateActive(Active active) {
        return activeMapper.updateActive(active);
    }

    @Override
    public Active selectActiveById(String activeId) {
        return activeMapper.selectActiveById(activeId);
    }

    @Override
    public List<Active> selectActiveList(Active active, int status) {
        List<Active> actives = activeMapper.selectActiveList(active);
        switch (status) {
            case ActiveConsts.NOT_BEGIN:
                return actives.stream().parallel()
                        .filter(a -> a.getStartTime() != null)
                        .filter(a -> a.getStartTime().after(DateUtils.getNowDate()))
                        .collect(Collectors.toList());
            case ActiveConsts.BEGIN:
                return actives.stream().parallel()
                        .filter(a -> a.getStartTime() != null)
                        .filter(a -> a.getStartTime().before(DateUtils.getNowDate()))
                        .filter(a -> a.getEndTime() != null)
                        .filter(a -> a.getEndTime().after(DateUtils.getNowDate()))
                        .collect(Collectors.toList());
            case ActiveConsts.FINISH:
                return actives.stream().parallel()
                        .filter(a -> a.getEndTime() != null)
                        .filter(a -> a.getEndTime().before(DateUtils.getNowDate()))
                        .collect(Collectors.toList());
        }
        return actives;
    }

    @Override
    public Integer userIsActiveLeader(String activeId, String userId) {
        return activeMapper.userIsActiveLeader(activeId,userId);
    }

    @Override
    public int updateActiveUserId(String activeUserId, String activeId) {
        return activeMapper.updateActiveUserId(activeUserId,activeId);
    }
}
